package net.sxnic.item.mdata.action;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@SuppressWarnings("serial")
public class Save extends MdataAction {
	
	private Logger logger = LoggerFactory.getLogger(PreCreate.class);
	private String showMessage = "";

	public void prepare() throws Exception {
		
		logger.debug("mdata.checkcontent:"+mdata.getCheckContent());
		if (StringUtils.isNotBlank(id)) {
			mdata = mdManager.get(id);
			// 获取大字段
			itemClob = clobManager.getEntityByBaseinfoId(id);
			// 获取经费
			itemFund = fundManager.getEntityByBaseinfoId(id);
			// 获取负责人
			itemPrincipal = principalManager.getEntityByBaseinfoId(id);
			//获取项目分阶段具体实施目标
			ssmbList = ssmbManager.findListByBaseinfoId(id);
			//获取参加人员
			peopleList =peopleManager.findListByBaseinfoId(id);
			//获取国际合作国外国内合作单位
			if("009".equals(mdata.getSoperCode()))
			{
				gwGjhzunitList = gjhzunitManager.findGwGjhzunit(id);
				gnGjhzunitList = gjhzunitManager.findGnGjhzunit(id);
			}
			//获取上传附件
			annex001List = annexManager.findListByFileSort("001", id);
			annex002List = annexManager.findListByFileSort("002", id);
		}

	}

	public String execute() throws Exception {
		
		if(StringUtils.isBlank(id))
		{
			return INPUT;
		}
		
		//设置学科代码和学科名称
		if(StringUtils.isNotBlank(level_1))
		{
			mdata.setSubject1Code(level_1);
			mdata.setSubject1Name(subjectManager.getValueByCode(level_1));
		}
		if(StringUtils.isNotBlank(level_2))
		{
			mdata.setSubject1Code(level_2);
			mdata.setSubject1Name(subjectManager.getValueByCode(level_2));
		}
		if(StringUtils.isNotBlank(level_3))
		{
			mdata.setSubject1Code(level_3);
			mdata.setSubject1Name(subjectManager.getValueByCode(level_3));
		}
		//设置负责人
		if(StringUtils.isNotBlank(itemPrincipal.getChinName()))
		{
			mdata.setPrincipalName(itemPrincipal.getChinName());
		}
		//保存主表数据
		mdManager.save(mdata);
		//保存子表数据
		clobManager.save(itemClob);
		fundManager.save(itemFund);
		principalManager.save(itemPrincipal);
		//保存国际合作合作单位信息
		if("009".equals(mdata.getSoperCode()))
		{
			if(gwGjhzunitList!=null&&gwGjhzunitList.size()>0)
			{
				gwGjhzunitList.get(0).setCoCountry(gwCountry1);
				gwGjhzunitList.get(0).setCoOrgname(gwOrgname1);
				gwGjhzunitList.get(0).setCoLeader(gwLeader1);
				gwGjhzunitList.get(0).setCoPhone(gwPhone1);
				gwGjhzunitList.get(0).setCoFax(gwFax1);
				gwGjhzunitList.get(0).setCoEmail(gwEmail1);
				gwGjhzunitList.get(1).setCoCountry(gwCountry2);
				gwGjhzunitList.get(1).setCoOrgname(gwOrgname2);
				gwGjhzunitList.get(1).setCoLeader(gwLeader2);
				gwGjhzunitList.get(1).setCoPhone(gwPhone2);
				gwGjhzunitList.get(1).setCoFax(gwFax2);
				gwGjhzunitList.get(1).setCoEmail(gwEmail2);
			}
			
			if(gnGjhzunitList!=null&&gnGjhzunitList.size()>0)
			{

				gnGjhzunitList.get(0).setCoOrgname(gnOrgname1);
				gnGjhzunitList.get(0).setCoLeader(gnLeader1);
				gnGjhzunitList.get(0).setCoPhone(gnPhone1);
				gnGjhzunitList.get(0).setCoFax(gnFax1);
				gnGjhzunitList.get(0).setCoEmail(gnEmail1);
				gnGjhzunitList.get(1).setCoOrgname(gnOrgname2);
				gnGjhzunitList.get(1).setCoLeader(gnLeader2);
				gnGjhzunitList.get(1).setCoPhone(gnPhone2);
				gnGjhzunitList.get(1).setCoFax(gnFax2);
				gnGjhzunitList.get(1).setCoEmail(gnEmail2);
			}
			
			gjhzunitManager.save(gwGjhzunitList);
			gjhzunitManager.save(gnGjhzunitList);
		}

		//准备回显数据
		appYear = mdata.getAppYear();
		catalogAlias = mdata.getCatalog1();
		catalogCode = mdata.getCatalog2();
		//JSP对应的页面年份
		jspYear= dyManager.findJspYear(appYear, catalogAlias, catalogCode);
		//${jspYear}/${catalogAlias}/${catalogCode}/edit.jsp
		jspCatalog = operationManager.findJspCatalog(mdata.getCatalog1(), mdata.getCatalog2(), mdata.getSoperCode());
	
		System.out.println(ssmbList.size());
		//提交
		if("submit".equals(optCate)){
		
			try {
				if(!vldSubmit()||!validateGGP()||!valFunds()) {//提交验证
					addActionMessage(valTip);
					return INPUT;
				}
				signal(taskManager.get(taskId), "提交");
			} catch (Exception e) {
				e.printStackTrace();
				return INPUT;
			}
			msg ="submitSuccess";
			return "approve_list";
		}
		
		

		logger.debug(jspYear+"/"+catalogAlias+"/"+catalogCode+"/edit.jsp");
		if(!"none".equals(showMessage))
		{
			addActionMessage("保存成功");
		}
		//重置
		showMessage = "";
		return SUCCESS;
	}

	public String getShowMessage() {
		return showMessage;
	}

	public void setShowMessage(String showMessage) {
		this.showMessage = showMessage;
	}

}
